// 算一算 部分习题数组 - 两位数加减
export function getStudySYS(studyType, num) {
  let ufuncArrAll = ['+', '-', 'x', '÷'] // 运算法则 所有
  let ufuncArr1 = ['x', '÷'] // 运算法则 乘除
  let ufuncArr2 = ['+', '-'] // 运算法则 加减
  let studyAll = []
  for (let i = 1; i <= num; i++) {
    // 随机得到 ufuncArrAll 运算规则
    let ufunc1 = ''
    let index1 = Math.floor(Math.random() * ufuncArrAll.length)
    ufunc1 = ufuncArrAll[index1]
    if (ufunc1 == 'x' || ufunc1 == '÷') {
      // 随机得到 一个 1-9 的随机数
      let num2 = Math.floor(Math.random() * 9 + 1) + 1
      // 根据 num2 和左侧运算规则 得到 num1 的取值范围
      let num1Max = Math.floor(studyType / num2)
      // 随机得到 x 和 ÷ 的小范围计算规则
      let studyCC = []
      if (ufunc1 == 'x') {
        let num1 = Math.floor(Math.random() * (num1Max - 1) + 1) + 1
        studyCC[3] = num1 * num2
        studyCC[0] = num1
        studyCC[1] = 'x'
        studyCC[2] = num2
      } else if (ufunc1 == '÷') {
        let result = Math.floor(Math.random() * num1Max + 1)
        studyCC[3] = result
        studyCC[0] = result * num2
        studyCC[1] = '÷'
        studyCC[2] = num2
      }
      // studyCC的位置 默认在前面
      let ccPosArr = ['before', 'after']
      let ccPos = Math.floor(Math.random() * ccPosArr.length)
      if (ccPosArr[ccPos] == 'before') {
        // 随机得到 另外一个 运算规则
        let ufunc2 = ''
        let index2 = Math.floor(Math.random() * ufuncArr2.length)
        ufunc2 = ufuncArr2[index2]
        if (ufunc2 == '+') {
          // 根据 studyCC 和 ccPosArr[ccPos] 和 ufunc2 得到 num3 的取值范围
          let num3Max = studyType - studyCC[3]
          // 随机得到 num3
          let num3 = Math.floor(Math.random() * num3Max + 1)
          let result = studyCC[3] + num3
          let pushResult = studyCC.slice(0, 3).concat([ufunc2, num3, result])
          studyAll.push(pushResult)
        } else if (ufunc2 == '-') {
          // 随机得到 num3
          let num3 = Math.floor(Math.random() * studyCC[3])
          let result = studyCC[3] - num3
          let pushResult = studyCC.slice(0, 3).concat([ufunc2, num3, result])
          studyAll.push(pushResult)
        }
      } else if (ccPosArr[ccPos] == 'after') {
        // 随机得到 另外一个 运算规则
        let ufunc2 = ''
        let index2 = Math.floor(Math.random() * ufuncArr2.length)
        ufunc2 = ufuncArr2[index2]
        if (ufunc2 == '+') {
          // 根据 studyCC 和 ccPosArr[ccPos] 和 ufunc2 得到 num3 的取值范围
          let num3Max = studyType - studyCC[3]
          // 随机得到 num3
          let num3 = Math.floor(Math.random() * num3Max + 1)
          let result = num3 + studyCC[3]
          let pushResult = [num3, ufunc2].concat(studyCC.slice(0, 3)).concat([result])
          studyAll.push(pushResult)
        } else if (ufunc2 == '-') {
          // 随机得到 num3
          let num3 = Math.floor(Math.random() * (studyType - studyCC[3]) + studyCC[3] + 1)
          let result = num3 - studyCC[3]
          let pushResult = [num3, ufunc2].concat(studyCC.slice(0, 3)).concat([result])
          studyAll.push(pushResult)
        }
      }
    } else if (ufunc1 == '+' || ufunc1 == '-') {
      let studyCC = []
      if (ufunc1 == '+') {
        let num1Min = Math.floor(studyType / 3)
        let num1Max = Math.floor(studyType / 3) * 2
        let num1 = Math.floor(Math.random() * (num1Max - num1Min) + num1Min + 1)
        let num2 = Math.floor(Math.random() * (studyType - num1) + 1)
        studyCC[0] = '( ' + num1
        studyCC[1] = '+'
        studyCC[2] = num2 + ' )'
        studyCC[3] = num1 + num2
      } else if (ufunc1 == '-') {
        let num1Min = Math.floor(studyType / 3)
        let num1 = Math.floor(Math.random() * (studyType - num1Min) + num1Min + 2)
        let num2 = Math.floor(Math.random() * (num1 - 1))
        studyCC[0] = '( ' + num1
        studyCC[1] = '-'
        studyCC[2] = num2 + ' )'
        studyCC[3] = num1 - num2
      }
      if (studyCC[3] > studyType / 2) {
        let num3Arr = []
        for (let i = 2; i <= studyCC[3]; i++) {
          if (studyCC[3] % i == 0) { num3Arr.push(i) }
        }
        let num3Index = Math.floor(Math.random() * num3Arr.length)
        let num3 = num3Arr[num3Index]
        let num4 = studyCC[3] / num3
        let pushResult = studyCC.slice(0, 3).concat(['÷', num3, num4])
        studyAll.push(pushResult)
      } else {
        // studyCC的位置 默认在前面
        let ccPosArr = ['before', 'after']
        let ccPos = Math.floor(Math.random() * ccPosArr.length)
        // 随机得到 另外一个 运算规则
        let ufunc2 = ''
        let index2 = Math.floor(Math.random() * ufuncArr1.length)
        ufunc2 = ufuncArr1[index2]
        if (ufunc2 == 'x') {
          let num3Max = Math.floor(studyType / studyCC[3])
          let num3 = Math.floor(Math.random() * num3Max + 1) + 1
          let num4 = studyCC[3] * num3
          if (ccPosArr[ccPos] == 'before') {
            let pushResult = studyCC.slice(0, 3).concat(['x', num3, num4])
            studyAll.push(pushResult)
          } else if (ccPosArr[ccPos] == 'after') {
            let pushResult = [num3, 'x'].concat(studyCC.slice(0, 3)).concat([num4])
            studyAll.push(pushResult)
          }
        } else if (ufunc2 == '÷') {
          if (ccPosArr[ccPos] == 'before') {
            let num3Arr = []
            for (let i = 2; i <= studyCC[3]; i++) {
              if (studyCC[3] % i == 0) { num3Arr.push(i) }
            }
            let num3Index = Math.floor(Math.random() * num3Arr.length)
            let num3 = num3Arr[num3Index]
            let num4 = studyCC[3] / num3
            let pushResult = studyCC.slice(0, 3).concat(['÷', num3, num4])
            studyAll.push(pushResult)
          } else if (ccPosArr[ccPos] == 'after') {
            let num3Max = Math.floor(studyType / studyCC[3])
            let num4 = Math.floor(Math.random() * num3Max + 1) + 1
            let num3 = num4 * studyCC[3]
            let pushResult = [num3, '÷'].concat(studyCC.slice(0, 3)).concat([num4])
            studyAll.push(pushResult)
          }
        }
      }
    }
  }
  return studyAll
}

// 填一填 部分习题数组
export function getStudyTYT(studyType, num) {
  let ufuncArrAll = ['+', '-', 'x', '÷'] // 运算法则 所有
  let ufuncArr1 = ['x', '÷'] // 运算法则 乘除
  let ufuncArr2 = ['+', '-'] // 运算法则 加减
  let studyAll = []
  for (let i = 1; i <= num; i++) {
    let studyItem = []
    // 随机得到 num2
    let num2 = Math.floor(Math.random() * 9 + 1) + 1
    studyItem[2] = num2
    // 随机得到 左侧运算规则
    let ufunc1 = ''
    let index1 = Math.floor(Math.random() * ufuncArr1.length)
    ufunc1 = ufuncArr1[index1]
    studyItem[1] = ufunc1
    // 根据 num2 和左侧运算规则 随机得到 num1
    let num1Max = Math.floor(studyType / num2)
    if (ufunc1 == 'x') {
      let num1 = Math.floor(Math.random() * (num1Max - 1) + 1) + 1
      studyItem[7] = num1 * num2
      studyItem[0] = num1
    } else if (ufunc1 == '÷') {
      let result = Math.floor(Math.random() * num1Max + 1)
      studyItem[7] = result
      studyItem[0] = result * num2
    }
    // 随机得到 右侧运算规则
    let ufunc2 = ''
    let index2 = Math.floor(Math.random() * ufuncArr2.length)
    ufunc2 = ufuncArr2[index2]
    studyItem[4] = ufunc2
    // 根据 studyItem[7] 和右侧运算规则 随机得到 num3
    let num3 = 0
    if (ufunc2 == '+') {
      num3 = Math.floor(Math.random() * (studyItem[7] - 1) + 1)
    } else if (ufunc2 == '-') {
      let jieguo = studyType - studyItem[7]
      num3 = Math.floor(Math.random() * jieguo + studyItem[7] + 1)
    }
    studyItem[3] = num3
    // 根据 studyItem[7] 和右侧运算规则 和num3 得到 num4
    let num4 = 0
    if (ufunc2 == '+') {
      num4 = studyItem[7] - num3
    } else if (ufunc2 == '-') {
      num4 = num3 - studyItem[7]
    }
    studyItem[5] = num4
    // 随机隐藏 num1、 num2、 num3 或者 num4 数字
    let noShowArr = ['num1', 'num2', 'num3', 'num4']
    let noShow = Math.floor(Math.random() * noShowArr.length)
    studyItem[6] = noShowArr[noShow]
    // 乘除法的位置 默认在等号的左侧
    let ccPosArr = ['before', 'after']
    let ccPos = Math.floor(Math.random() * ccPosArr.length)
    studyItem[8] = ccPosArr[ccPos]
    // studyItem 添加到所有的试题中
    if (studyItem[8] == 'after') {
      let beforeNums = studyItem.slice(3, 6)
      let afterNums = studyItem.slice(0, 3)
      let endResult = studyItem.slice(6)
      let pushResult = beforeNums.concat(afterNums).concat(endResult)
      studyAll.push(pushResult)
    } else {
      studyAll.push(studyItem)
    }
  }
  return studyAll
}

// 除法 部分习题数组
export function getStudyCF(studyType, num) {
  let studyAll = []
  for (let i = 1; i <= num; i++) {
    let studyItem = []
    // 随机得到 num2
    let num2 = Math.floor(Math.random() * 9 + 1) + 1
    studyItem[2] = num2
    // 运算规则为 除法
    let ufunc = '÷'
    studyItem[1] = ufunc
    // 根据 num2 和 ufunc 随机得到 num1
    let num1 = Math.floor(Math.random() * studyType + num2 + 1)
    studyItem[0] = num1
    // 根据 左侧的运算得出 右侧的结果
    let num3 = Math.floor(num1 / num2)
    studyItem[3] = num3
    let num4 = Math.floor(num1 % num2)
    studyItem[4] = num4
    // 随机隐藏 num1、 num2、 num3 或者 num4 数字
    let noShowArr = ['num1', 'num2', 'num3', 'num4']
    let noShow = Math.floor(Math.random() * noShowArr.length)
    studyItem[5] = noShowArr[noShow]
    studyAll.push(studyItem)
  }
  return studyAll
}
